const app = getApp();
var interval = null //倒计时函数
Page({
  data:{
    phone:"",
    time:'获取验证码',
    currentTime:61,
    loading:false 
  },
  toLogin:function(){
    wx.reLaunch({
      url: '../login/login',
    })
  },
  onShow:function(){
    this.getAgreeMent();
    //获取公司信息
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetCompanyInfo',
      data: {
        tenantName: app.globalData.tenancyName
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          _this.setData({
            companyName: res.data.result.companyName,
            logoPath: app.globalData.logoPath + res.data.result.logoPath
          })
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getphone:function(e){
    this.setData({
      phone:e.detail.value
    })
  },
  getname: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  getIdNum: function (e) {
    this.setData({
      idnumber: e.detail.value
    })
  },
  //是否同意
  checkboxChange: function (e) {
    if (e.detail.value.length == 0) {
      this.setData({
        check: 0  //未选
      })
    }
    else {
      this.setData({
        check: 1  //已选
      })
    }
    // console.log(this.data.check)
  },
  // 条款
  getAgreeMent: function () {
    this.setData({
      title: app.globalData.agreementTitle
    })
  },
  // 协议
  agreeMent: function () {
    wx.navigateTo({
      url: '../agreement/agreement',
    })
  },
  // 验证身份证
  isCardNoTrue: function (card) {
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X 
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (reg.test(card) === false) {
      wx.showModal({
        content: '身份证格式不正确',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return false;
    }
    return true;
  },
  //kwq
  toYanzheng: function(){
    wx.reLaunch({
      url: '../unregister/unregister',
    })
  },
  formSubmit: function (e) {
    console.log(e.detail.value.phone);
    console.log(e.detail.value.pwd);
    console.log(e.detail.value.code);
    this.setData({
      phone: e.detail.value.phone,
      pwd: e.detail.value.pwd,
      code: e.detail.value.code,
      idnumber: e.detail.value.idnumber,
      name: e.detail.value.name,
     
    })
    var _this = this
    if(this.data.name){
      if (this.data.idnumber){
        if (this.isCardNoTrue(this.data.idnumber)){
          if (this.data.phone && this.data.phone.length == 11) {
            if (this.data.code) {
              if (this.data.phone && this.data.pwd) {
                if (this.data.pwd.length >= 6 && this.data.pwd.length <= 18) {
                  if (this.data.check == 1) {
                    _this.setData({
                      loading: true
                    })
                    wx.request({
                      url: app.globalData.path + 'services/ValidateCirc',
                      data: {
                        "name": e.detail.value.name,
                        "identityCard": e.detail.value.idnumber,
                        "phoneNumber": e.detail.value.phone,  //手机号码
                        "password": e.detail.value.pwd,  //密码
                        "code": e.detail.value.code,//验证码
                        "securityCode": this.data.securityCode,  //安全码
                        "tenancyName": app.globalData.tenancyName//租户名称
                      },
                      method: 'POST',
                      header: {
                        'content-type': 'application/json' // 默认值
                      },
                      success: function (res) {
                      console.log(res.data)
                      //kwq
                       
                      
                      if(res.data.success == true){
                        _this.setData({
                          loading: false
                        })
                       
                        if (res.data.result.isValidate == true){//true跳转到答题认证
                          wx.reLaunch({
                            url: '../veranswer/veranswer?id=' + res.data.result.id + '&tenancyName=' + app.globalData.tenancyName + '&password=' + e.detail.value.pwd+'&phone=' + e.detail.value.phone + '&name=' + e.detail.value.name + '&idnumber=' + e.detail.value.idnumber,
                          })
                        }else{
                          wx.request({
                            url: app.globalData.path + 'services/RegisterCirc',
                            data: {
                              id: res.data.result.id,
                            },
                            method: 'POST',
                            header: {
                              'content-type': 'application/json' // 默认值
                            },
                            success: function (res) {
                              console.log(res)
                              if (res.data.result.token) {
                                app.globalData.authorization = res.data.result.token;
                                wx.reLaunch({
                                  url: '../addcard/addcard?phone=' + e.detail.value.phone + '&name=' + e.detail.value.name + '&idnumber=' + e.detail.value.idnumber + '&from=register',
                                })
                              } else {
                                if (res.data.error.code == 1) {
                                  wx.showToast({
                                    title: '手机号已被注册，请登录',
                                    icon: 'none',
                                    duration: 2000
                                  })
                                } else if (res.data.error.code == 2) {
                                  wx.showToast({
                                    title: '身份证号已被注册，请登录',
                                    icon: 'none',
                                    duration: 2000
                                  })
                                } else {
                                  wx.showToast({
                                    title: res.data.error.message,
                                    icon: 'none',
                                    duration: 2000
                                  })
                                }
                              }
                            }
                          })  
                        }
                      } else if (res.data.error.code == -3){
                        _this.setData({
                          loading: false
                        })

                      
                        wx.navigateTo({
                          url: '../unregister/unregister',
                        })
                      }else{
                        _this.setData({
                          loading: false
                        })

                        wx.showToast({
                          title: res.data.error.message,
                          icon: 'none',
                          duration: 2000
                        })
                      }

                      }
                    })
                  } else {
                    wx.showModal({
                      content: '未同意协议',
                      showCancel: false,
                      success: function (res) {
                        if (res.confirm) {
                          // console.log('用户点击确定')
                        }
                      }
                    });
                  }
                } else {
                  wx.showModal({
                    content: '密码格式错误！6~18位密码',
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      }
                    }
                  });
                }
              } else {
                wx.showModal({
                  content: '手机号或密码不能为空！',
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    }
                  }
                });
              }
            } else {
              wx.showModal({
                content: '验证码不能为空!',
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  }
                }
              });
            }
          } else {
            wx.showModal({
              content: '请输入正确的手机号！',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                }
              }
            });
          }
        }
      }else{
        wx.showModal({
          content: '请输入身份证号',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      }
    }else{
      wx.showModal({
        content: '请输入员工姓名',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }
  },
  sendCode:function() {
    const _this = this;
    if (this.data.phone && this.data.phone.length == 11){
      this.settimeD();
      _this.setData({
        disabled: true
      })
      wx.request({
        url: app.globalData.path + 'services/app/applets/SendSMSCode',
        data: {
          phoneNumber: this.data.phone,
          SendType: 1
        },
        method: 'POST',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          console.log(res.data.result.securityCode)
          if (res.data.success) {
            _this.setData({
              securityCode: res.data.result.securityCode
            })
            if (res.data.result.sendResultType == 2) {
              wx.showToast({
                title: '手机号不存在',
                icon: 'none',
                duration: 2000
              })
            } else if (res.data.result.sendResultType == 3) {
              wx.showToast({
                title: '操作频繁',
                icon: 'none',
                duration: 2000
              })
            } else if (res.data.result.sendResultType == 4 || res.data.result.sendResultType == 5) {
              wx.showToast({
                title: '短信模板错误',
                icon: 'none',
                duration: 2000
              })
            }
          } else {
            wx.showToast({
              title: '发送失败，请重试',
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }else{
      wx.showModal({
        content: '请输入正确格式的手机号',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }
  },
  //倒计时
  settimeD: function(options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },
})